﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils.Common
{
    public class Pager
    {
        
        /// <summary>
        /// 获取用户自定义分页
        /// </summary>
        /// <param name="recordCount">记录总条数</param>
        /// <param name="pageSize">每页多少条</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pagefile">当前新闻的分类</param>
        /// <returns>返回自定义分页</returns>
        public static string GetPager(int recordCount, int pageSize, int pageIndex, string pagefile)
        {
            if (recordCount <= pageSize)
            {
                return "";
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            
            //共多少页
            int pageCount = recordCount / pageSize;
            if (recordCount % pageSize != 0)
            {
                pageCount++;
            }
            //sb.AppendFormat("<span>当前第 {0} 页 共 {1} 页</span>",pageIndex,pageCount);

            //上一页

            if (pageIndex > 1)
            {
                //sb.AppendFormat("<li class=\"c_hs\"><a href=\"{0}page=1\" class=\"pre\">首页</a></li>", pagefile);
                sb.AppendFormat("<li class=\"z_px\"><a href=\"{0}page={1}\" class=\"pre\">上一页</a></li>", pagefile, pageIndex - 1);
            }
            else
            {
                //sb.AppendFormat("<span><a>{0}</a></span>", "首页");
                //sb.AppendFormat("<a class=\"ButOne\">{0}</a>", "上一页");
            }

            //当小于11页的时候执行

            if (pageCount < 11)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    GetCurrentString(sb, i, pageIndex, pagefile);
                }
            }
            else
            {
                int first = 2;
                int prev = pageIndex - 2;//前两页

                int next = pageIndex + 2;//后两页

                int last = pageCount - 1;

                //前两页

                for (int i = 1; i < 3; i++)
                {
                    GetCurrentString(sb, i, pageIndex, pagefile);
                }
                if (prev > first + 1)
                {
                    sb.Append("<li class=\"c_hs\"><a style='background:none;color:#000;'>…</a></li>");
                }
                //中间页

                for (int i = prev; i <= next; i++)
                {
                    if (i > first && i < last)
                    {
                        GetCurrentString(sb, i, pageIndex, pagefile);
                    }
                }
                if (last > next + 1)
                {
                    sb.Append("<li class=\"c_hs\"><a  style='background:none;color:#000;'>…</a></li>");
                }
                //后两页

                for (int i = last; i <= pageCount; i++)
                {
                    GetCurrentString(sb, i, pageIndex, pagefile);
                }
            }
            //下一页

            if (pageIndex < pageCount)
            {

                sb.AppendFormat("<li class=\"z_px\"><a href=\"{0}page={1}\" class=\"next\">下一页</a></li>", pagefile, pageIndex + 1);
                //sb.AppendFormat("<li class=\"c_hs\"><a href=\"{0}page={1}\" class=\"next\">尾页</a></li>", pagefile, pageCount);
            }
            else
            {
                //sb.AppendFormat("<a class=\"ButTwo\">{0}</a>", "下一页");
                //sb.AppendFormat("<span><a>{0}</a></span>", "尾页");
            }
            
            return sb.ToString();
        }

        /// <summary>
        /// 获取当前字符串
        /// </summary>
        /// <param name="sb">字符对象</param>
        /// <param name="i">循环页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pagefile">当前文件及参数</param>
        static void GetCurrentString(System.Text.StringBuilder sb, int i, int pageIndex, string pagefile)
        {
            if (i == pageIndex)
            {
                sb.AppendFormat("<li class=\"c_hsc\"><a>{0}</a></li>", i);
            }
            else
            {
                sb.AppendFormat("<li class=\"c_hs\"><a href=\"{0}page={1}\">{1}</a></li>", pagefile, i);
            }
        }
    }
}
